package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.map.L1Map;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class L1CallCheck implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1CallCheck.class);
  
  private final int[] HEADING_TABLE_X = new int[] { 0, 1, 1, 1, 0, -1, -1, -1 };
  
  private final int[] HEADING_TABLE_Y = new int[] { -1, -1, 0, 1, 1, 1, 0, -1 };
  
  public static L1CommandExecutor getInstance() {
    return new L1CallCheck();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer tok = new StringTokenizer(arg);
      String name = tok.nextToken();
      L1PcInstance tgpc = World.get().getPlayer(name);
      if (tgpc != null) {
        L1Map map = pc.getMap();
        int locX = pc.getX();
        int locY = pc.getY();
        int heading = pc.getHeading();
        locX += this.HEADING_TABLE_X[heading];
        locY += this.HEADING_TABLE_Y[heading];
        heading = (heading + 4) % 4;
        short mapId = pc.getMapId();
        boolean isExsistCharacter = false;
        Iterator<L1Object> iterator = World.get().getVisibleObjects((L1Object)pc, 1).iterator();
        while (iterator.hasNext()) {
          L1Object object = iterator.next();
          if (object instanceof L1Character) {
            L1Character cha = (L1Character)object;
            if (cha.getX() == locX && cha.getY() == locY && cha.getMapId() == mapId) {
              isExsistCharacter = true;
              break;
            } 
          } 
        } 
        if ((locX == 0 && locY == 0) || !map.isPassable(locX, locY, null) || isExsistCharacter) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("你的前方有障碍物或对象，请空出1格再试。"));
          return;
        } 
        L1Teleport.teleport(tgpc, locX, locY, mapId, heading, true, 1);
        execute(pc, tgpc);
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("没有此玩家或对方没有在线。"));
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
    } 
  }
  
  private void execute(L1PcInstance pc, L1PcInstance tgpc) {
    if (tgpc.getCurrentHp() > 50) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("[外挂自动加血测试]玩家(" + tgpc.getName() + ") " + "测试开始。"));
      execute(tgpc);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("测试结束。"));
    } else {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("[外挂自动加血测试]玩家(" + tgpc.getName() + ") " + "血量低于50 自动加满血并继续测试。"));
      tgpc.setCurrentHp(tgpc.getMaxHp());
      execute(tgpc);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("测试结束。"));
    } 
  }
  
  private void execute(L1PcInstance tgpc) {
    tgpc.setCurrentHp(50);
  }
}
